<template>
    <div>
        <Bread></Bread>
        <el-card>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-input placeholder="请输入内容" class="input-with-select" v-model="query.query">
                        <el-button slot="append" icon="el-icon-search" @click="getGoods"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="7">
                    <el-button type="primary" @click="addGoods">添加商品</el-button>
                </el-col>
            </el-row>
            <!-- 表格 -->
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column type="index" label="#">
                </el-table-column>
                <el-table-column prop="goods_name" label="商品名称" width="180">
                </el-table-column>
                <el-table-column prop="goods_price" label="价格" width="180">
                </el-table-column>
                <el-table-column prop="goods_weight" label="重量" width="180">
                </el-table-column>
                <el-table-column prop="upd_time" label="创建时间" width="180">
                </el-table-column>
                <el-table-column prop="address" label="操作" width="280">
                    <template slot-scope="scope">
                        <el-button type="warning" size="mini" @click="editDialog(scope.row.goods_id)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="delGoods(scope.row.goods_id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 编辑对话框 -->
            <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
                <el-form :model="editForm">
                    <el-form-item label="商品名称" label-width="80px">
                        <el-input v-model="editForm.goods_name" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="商品价格" label-width="80px">
                        <el-input v-model="editForm.goods_price" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="商品数量" label-width="80px">
                        <el-input v-model="editForm.goods_number" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="商品重量" label-width="80px">
                        <el-input v-model="editForm.goods_weight" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editGoods">确 定</el-button>
                </div>
            </el-dialog>
            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="query.pagenum" :page-sizes="[3, 5, 7, 9]" :page-size="query.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
import { mapState } from 'vuex';
import mixin from '@/mixin/mixin'
import qs from 'querystring'
export default {
    mixins: [mixin],
    data() {
        return {
            tableData: [],
            query: {
                query: '',
                pagenum: 1,
                pagesize: 5
            },
            editForm: {
                "goods_name": "",
                "goods_price": '',
                "goods_number": '',
                "goods_weight": '',
            },
            dialogFormVisible: false,
            editId: 0,
            total:0,//商品数据列表总条数
        }
    },
    mounted() {
        // console.log(this.tableData);
        this.getGoods();
    },
    computed: {
        ...mapState(['goodsObj'])
    },
    methods: {
        addGoods() {
            // alert(111);
            /* 获取路由对象里的路由数据 */
            let routes = this.$router.getRoutes();
            // console.log(routes);
            let index = routes.findIndex(item => {
                return item.path == '/addGoods'
            });
            // console.log(index);
            if (index == -1) {
                let obj = {
                    path: '/addGoods',
                    name: 'AddGoods',
                    component: () => import('@/components/home/AddGoods.vue')
                }
                /* 将新的addGoods路由对象添加到路由数组里 */
                this.$router.addRoute('Home', obj);
                console.log(this.$router.getRoutes());
                /* 将新的addGoods路由对象保存到VueX里，防止刷新页面数据丢失 */
                this.$store.commit('routeModule/addGoodRoute', obj);
            };
            /* 存储面包屑导航数据 */
            let obj = { oneName: '商品列表', twoName: '添加商品', path: '/addGoods' };
            sessionStorage.setItem('menu', JSON.stringify(obj));
            // console.log(this.$store._modules.root.state.routeModule.route);
            /* 跳转到addGoods页面 */
            this.$router.push('/addGoods');
        },
        /* 获取商品列表数据 */
        async getGoods() {
            let res = await this.$http.get('/goods?' + qs.stringify(this.query));
            // console.log(res);
            this.tableData = res.data.goods;
            this.total = res.data.total;
        },
        /* 删除 */
        delGoods(id) {
            // console.log(id);
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const data = await this.$http.delete(`goods/${id}`);
                console.log(data);
                if (data.meta.status == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getGoods();
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        /* 编辑回显 */
        editDialog(id) {
            // console.log(id);
            this.dialogFormVisible = true;
            this.editId = id;
        },
        /* 编辑修改内容 */
        async editGoods() {
            let res = await this.$http.put(`goods/${this.editId}`, this.editForm);
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg);
                this.getGoods();
                this.dialogFormVisible = false;
            } else {
                this.$message.error(res.meta.msg);
            }
        },
        /* 当前页发生改变触发 */
        handleCurrentChange(num) {
            console.log(num);
            this.query.pagenum = num;
            this.getGoods();
        },
        /* 每页条数发生改变触发 */
        handleSizeChange(size) {
            console.log(size);
            this.query.pagesize = size;
            this.getGoods();
        }
    },

}
</script>

<style lang="scss" scoped>

</style>